<?xml version='1.0' encoding='UTF-8' ?>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui">

<f:view encoding="ISO-8859-1">
	<h:head>
		<title>Produtos cadastrados</title>
		<h:outputStylesheet library="css" name="styles.css" />
		<h:outputScript library="js" name="scripts.js" />
	</h:head>
	<h:body>
		<h:form id="frmSale" styleClass="frm"
			rendered="#{loginBean.isLogged() and loginBean.hasPermission('CadGeneral')}">
			
			<ui:include src="../templates/menu.xhtml" />
			
			<p:growl id="messages" showDetail="true" sticky="true"/>
				
			<p:panel id="panelSale" header="Vendas" styleClass="cadPanelPrincipal">
				<h:panelGrid id="panelGrid" columns="8" cellspacing="5">
				
					<h:outputLabel value="Permitir a escolha do cliente? "/>
					<p:selectBooleanCheckbox value="#{saleBean.allowSelectClient}">
						<p:ajax update="messages acClientSelect"/>
					</p:selectBooleanCheckbox>
				
					<h:outputLabel id="lblClientName" value="Nome do cliente:"
						styleClass="outPutLabel"/>
					<p:autoComplete id="acClientSelect" label="Nome do cliente"
						value="#{saleBean.client}" completeMethod="#{clientBean.completeClients}"  
                        var="cli" itemLabel="#{cli.person.name}" itemValue="#{cli}"
                        converter="clientConverter" forceSelection="true"
						disabled="#{!saleBean.allowSelectClient}">
						
						<p:ajax event="itemSelect" listener="#{saleBean.acClientSelect}"/>
					
					</p:autoComplete>
					
					<h:outputLabel id="lblUserName" value="Usuário: "
						styleClass="outPutLabel" />
					<p:inputText id="txtUserName" label="Usuário" disabled="true"
						styleClass="inputText" value="#{loginBean.user.login}"/>
					
					<h:outputLabel value=""/>
					<h:outputLabel value=""/>
					
					<h:outputLabel id="lblProductBarcode" value="Código de barras: "
						styleClass="outPutLabel" />
					<p:inputText id="txtProductBarcode" label="Código de barras"
						styleClass="inputText" value="#{saleBean.barcode}"
						onkeydown="if ((event.keyCode==13)||(event.keyCode==9)){ this.blur(); return false; }">
						<p:ajax event="blur" update="txtProductName txtProdPrice" listener="#{saleBean.onTxtProductBarcodeBlur}"/>
					</p:inputText>
				
					<h:outputLabel id="lblProductName" value="Produto: "
						styleClass="outPutLabel" />
					<p:inputText id="txtProductName" label="Produto" disabled="true"
						styleClass="inputText" value="#{saleBean.product.name}">
					</p:inputText>
					
					<h:outputLabel id="lblProductNumber" value="Número: "
						styleClass="outPutLabel" />
					<p:inputText id="txtProductNumber" label="Número"
						styleClass="inputText" value="#{saleBean.proNumber}">
					</p:inputText>
				
					<h:outputLabel id="lblProdPrice" value="Preço: " styleClass="outPutLabel" />
					<h:outputText id="txtProdPrice" value="#{saleBean.product.price}" label="Preço">
						<f:convertNumber maxFractionDigits="2" groupingUsed="true" currencySymbol="R$"
							maxIntegerDigits="4" type="currency" currencyCode="BRL"/>
					</h:outputText>
				</h:panelGrid>
				<p:commandButton id="cmdButtonAddToSaleList" value="Adicionar à venda" actionListener="#{saleBean.onAddToSaleList}"
					update=":frmSale:tblProducts :frmSale:messages :frmSale:txtSaleTotal" icon="ui-icon-disk"/>
				
				<p:dataTable id="tblProducts" var="pro" value="#{saleBean.productList}"
					emptyMessage="A venda atual ainda não possui produtos!"
					rowKey="#{pro.prcode}" selection="#{saleBean.prodAuxTbl}" selectionMode="single">
					
					<p:column headerText="CÓDIGO">
						#{pro.barcode}
					</p:column>
					<p:column headerText="NOME">
						#{pro.name}
					</p:column>
					<p:column headerText="NÚMERO">
						#{pro.proNumber}
					</p:column>
					<p:column headerText="PREÇO">
						<h:outputText value="#{pro.price}">
							<f:convertNumber maxFractionDigits="2" groupingUsed="true" currencySymbol="R$"
								maxIntegerDigits="4" type="currency" currencyCode="BRL"/>
						</h:outputText>
					</p:column>
					
					<f:facet name="footer">
						<p:commandButton id="delButton" value="Remover produto selecionado"
							actionListener="#{saleBean.onRemoveFromSaleList}" icon="ui-icon-trash"
							update=":frmSale:tblProducts :frmSale:messages :frmSale:txtSaleTotal"/>
					</f:facet>			
				</p:dataTable>
				<br/>
				<br/>
				<h:outputText value="TOTAL: "/>
				<p:inputText maxlength="15" size="20" id="txtSaleTotal" value="#{saleBean.total}"
					styleClass="inputText" label="TOTAL" disabled="true">
					<f:convertNumber maxFractionDigits="2" groupingUsed="true" currencySymbol="R$"
						maxIntegerDigits="4" type="currency" currencyCode="BRL"/>
				</p:inputText>
				
				<p:commandButton id="closeSale" value="Fechar venda" actionListener="#{saleBean.onCloseSale}" icon="ui-icon-calculator"/>
			</p:panel>
		</h:form>
		<h:form	rendered="#{!loginBean.isLogged() or !loginBean.hasPermission('CadGeneral')}">
			<h1 align="center">
				<h:outputText value="Acesso não autorizado a esta página!" />
			</h1>
			<h1 align="center">
				<h:graphicImage value="#{resource['images/general/forbidden.png']}" />
			</h1>
		</h:form>
		<ui:include src="../templates/footer.xhtml" />
	</h:body>
</f:view>
</html>